﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// WMS客户档案
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BXCustomerInfoController : ControllerBase
    {
        private readonly ILogger<BXCustomerInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con2;


        public BXCustomerInfoController(ILogger<BXCustomerInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con2 = _configuration.GetConnectionString("WMSDbConnection_ZH");

        }

        /// <summary>
        /// WMS客户档案
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostBXCustomerInfo([FromBody] BXCustomerInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            BXCustomerInfoRequestDto result = new BXCustomerInfoRequestDto();
            BXCustomerInfoResponseDto re = new BXCustomerInfoResponseDto();
            string ddate = strparm.ddate;

            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {
                if (ddate.Trim() != "")
                {

                }


                strSql = new StringBuilder();
                strSql.Append(@"select DISTINCT customer_code,customer_name from sales_order where 1=1 ");
                if (ddate.Trim() != "")
                {
                    strSql.Append(" and create_time='" + ddate.Trim() + "'");
                }
                else
                {
                    strSql.Append(" and create_time>'2023-01-01'");
                }
                DataTable dt_customer = DB.Query(strSql.ToString(), con2);
                if (dt_customer.Rows.Count <= 0)
                {
                    strMsg = "无数据，请检查清楚再调用接口";
                }
                else
                {
                    re.data = DB.TableToListModel<BXCustomerInfoResponseData>(dt_customer);
                }





            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "0";
            }
            else
            {
                re.status = "失败";
                re.code = "500";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
